using System;
using UnityEngine;

public class Notifications : BehaviourSingletonInit<Notifications>
{
	private GGNotifications notifications;

	public override void Init()
	{
		base.Init();
		notifications = GGNotifications.instance;
		OnApplicationPause(false);
		Debug.Log("Notifications initialized");
	}

	public void OnExitApp()
	{
		OnApplicationPause(true);
	}

	public DateTime getTime(double secondsFromNow)
	{
		DateTime result = DateTime.Now.AddSeconds(secondsFromNow);
		if (result.Hour > ConfigBase.instance.notificationLatestTime)
		{
			result = result.AddHours(ConfigBase.instance.notificationEarliestTime + 24 - result.Hour);
		}
		else if (result.Hour < ConfigBase.instance.notificationEarliestTime)
		{
			result = result.AddHours(ConfigBase.instance.notificationEarliestTime - result.Hour);
		}
		return result;
	}

	private void OnApplicationPause(bool paused)
	{
		if (paused && !GGPlayerSettings.instance.Model.notificationsOff)
		{
			notifications.CancelAllLocalNotifications();
			float num = 30f;
			float num2 = 120f;
			PlayerCareerStageDefinition.StageDefinition stageDefinition = ScriptableObjectSingleton<PlayerCareerStages>.instance.CurrentStage();
			double num3 = 0.0;
			GGNotifications.Notification notification;
			if (stageDefinition != null)
			{
				if (stageDefinition.subStages.Count == 0 && stageDefinition.type == PlayerCareerStageDefinition.StageType.OpponentChallenge)
				{
					TimeSpan timeTillNextMatch = stageDefinition.gameParams.GetTimeTillNextMatch(stageDefinition);
					float num4 = Mathf.Max(num * 60f, (float)timeTillNextMatch.TotalSeconds);
					if (timeTillNextMatch.TotalSeconds <= 0.0)
					{
						num4 = num2 * 60f;
					}
					num3 = num4;
					notification = new GGNotifications.Notification();
					if (stageDefinition.type == PlayerCareerStageDefinition.StageType.OpponentChallenge)
					{
						notification.alertAction = "Match against " + stageDefinition.gameParams.opponent.player.playerName + " starts now!";
					}
					else
					{
						notification.alertAction = "Training " + stageDefinition.gameParams.opponent.player.playerName + " starts now!";
					}
					notification.alertBody = "Tap To Play Table Tennis Now!";
					notification.fireDate = getTime(num4);
					notification.repeat = GGNotifications.RepeatInterval.Never;
					notifications.ScheduleLocalNotification(notification);
				}
				if (stageDefinition.subStages.Count > 0)
				{
					PlayerCareerStageDefinition.StageDefinition.SubStageDefinition activeSubstage = stageDefinition.GetActiveSubstage();
					if (activeSubstage != null)
					{
						TimeSpan timeTillNextMatch2 = activeSubstage.gameParams.GetTimeTillNextMatch(stageDefinition);
						float num5 = Mathf.Max(num * 60f, (float)timeTillNextMatch2.TotalSeconds);
						if (timeTillNextMatch2.TotalSeconds <= 0.0)
						{
							num5 = num2 * 60f;
						}
						num3 = Mathf.Max(num5, (float)num3);
						notification = new GGNotifications.Notification();
						notification.alertAction = activeSubstage.name + " against " + GameConstants.Instance.CountryForFlag((int)activeSubstage.gameParams.opponent.player.flag).countryName + " starts now!";
						notification.alertBody = "Tap To Play Table Tennis Now!";
						notification.fireDate = getTime(num5);
						notification.repeat = GGNotifications.RepeatInterval.Never;
						notifications.ScheduleLocalNotification(notification);
					}
				}
			}
			double num6 = num3 + 86400.0;
			notification = new GGNotifications.Notification();
			notification.alertAction = "Your Table Tennis Team needs you!";
			notification.alertBody = "Tap To Play Table Tennis Now!";
			notification.fireDate = getTime(num6);
			notification.repeat = GGNotifications.RepeatInterval.Week;
			notifications.ScheduleLocalNotification(notification);
			notification.repeat = GGNotifications.RepeatInterval.Never;
			notification.fireDate = getTime(num6 + 259200.0);
			notifications.ScheduleLocalNotification(notification);
		}
		else
		{
			notifications.CancelAllLocalNotifications();
		}
	}

	public void ClearNotifications()
	{
		GGNotifications.instance.CancelAllLocalNotifications();
	}
}
